package club.zynight.crawler.main;

import club.zynight.crawler.entity.SenicSpotEntity;
import club.zynight.crawler.utils.DataUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Mysql {
    public static void main(String[] args) throws Exception {
        //printTitle("http://www.poilist.cn/poi-list-景点-北京/0/");
       /* SenicSpotEntity entity = new SenicSpotEntity();
        DataUtils.setEntityParameterValue(11,"12.123456",entity);
        System.out.println(entity);*/

        List<SenicSpotEntity> list = printData("http://www.poilist.cn/poi-list-景点-北京/0/");
        for (SenicSpotEntity entity : list){
            System.out.println(entity);
        }
    }

    public static void printTitle(String url) throws IOException {
        Document document = Jsoup.parse(new URL(url),1000);
        Element title = document.getElementsByTag("title").first();
        System.out.println(title.text());
    }

    public static List<SenicSpotEntity> printData(String url) throws Exception {
        Document document = Jsoup.parse(new URL(url),1000);
        Elements elements = document.getElementsByTag("tr");//获取所有tr的行数据
        elements.remove(0);//删除第一行的标题栏
        List<SenicSpotEntity> list = new ArrayList();
        for(Element element : elements){
            Elements infos = element.getElementsByTag("td");
            SenicSpotEntity entity = new SenicSpotEntity();
            int index = 0;
            for(Element info : infos){
                DataUtils.setEntityParameterValue(index++,info.text(),entity);//存储数据
            }
            list.add(entity);
        }
        return list;
    }

    public static void testDataUtils(int index,String value,SenicSpotEntity entity){
        DataUtils.setEntityParameterValue(index,value,entity);
    }

    public static void HttpClient(){

    }

}
